using EFCoreDemo.Models;
using EFCoreDemo.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EFCoreDemo.Controllers;

[ApiController]
[Route("[controller]")]
public class DemoController {
    private IDemoService _demoService;

    public DemoController(IDemoService demoService) {
        _demoService = demoService;
    }

    [Route("post")]
    [HttpPost]
    public async Task<ActionResult<DemoModel>>
        Post([FromBody] DemoModel model) {
        return await _demoService.AddAsync(model);
    }

    [Route("put")]
    [HttpPut]
    public async Task<ActionResult<DemoModel>> Put([FromBody] DemoModel model) {
        return await _demoService.UpdateAsync(model);
    }

    [Route("delete")]
    [HttpDelete]
    public async Task<ActionResult<DemoModel>> Delete(int id) {
        return await _demoService.DeleteAsync(id);
    }

    [Route("list")]
    [HttpGet]
    public async Task<ActionResult<IEnumerable<DemoModel>>> List() {
        return (await _demoService.ListAsync()).ToList();
    }
}